package bbs.net;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bbs.model.Subject;
import managers.VolleySingleton;

/**
 * Created by nimi on 2015/11/17.
 */
public class AllSubject {
     Download download;
     Delegate delegate;
    public Download getDownload() {
        return download;
    }

    public void setDownload(Download download) {
        this.download = download;
    }
    public Delegate getDelegate() {
        return delegate;
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void get() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/BBS/GetAllBBSTopic";
        RequestQueue requestQueue = VolleySingleton.getVolleySingleton(MyApplication.share()).getRequestQueue();
        StringRequest stringRequest =new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            public void onResponse(String response) {
                afterGet(response);
                if(delegate!=null)
                    delegate.onResponse(response,AllSubject.this);
            }}, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error, AllSubject.this);

            }
        }) {
                @Override
                protected Map<String, String> getParams () {
                Map map = new HashMap();
                return map;
            }
                @Override
                protected Response<String> parseNetworkResponse (NetworkResponse response){
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }

        };
        requestQueue.add(stringRequest);
    }
    // 获取下行数据后
    private void afterGet(String response) {
        //download = JSON.parseObject(response, Download.class);
            download = JSON.parseObject(response, Download.class);
           /* Gson gson = new GsonBuilder().setPrettyPrinting().create();
            JSONObject data = response.getGraphObject().getInnerJSONObject();
            download = gson.fromJson(response.toString(), MyPost.Download.class);*/

    }

    // 下行数据
    public static class Download
    {
        // 状态
        int status;
        // 所有的主题
        List<Subject> topics = new ArrayList<Subject>();
        public int getStatus() {
            return status;
        }
        public void setStatus(int status) {
            this.status = status;
        }
        public List<Subject> getTopics() {
            return topics;
        }
        public void setTopics(List<Subject> topics) {
            this.topics = topics;
        }


    }
    public static interface Delegate
    {
        public void onResponse(String response, AllSubject allSubject);
        public void onErrorResponse(VolleyError error, AllSubject allSubject);
    }
}
